package controller

import (
	"afupanel/dto"
	"afupanel/service"
	"github.com/gin-gonic/gin"
)

func LoginCheck(c *gin.Context) {
	var loginReq dto.LoginReq
	if err := c.ShouldBindJSON(&loginReq); err != nil {
		ReturnFailed(err.Error(), c)
		return
	}
	loginReq.ClientIP = GetClientIP(c)
	if token, err := service.LoginCheck(loginReq); err != nil {
		ReturnFailed(err.Error(), c)
		return
	} else {
		ReturnSuccessWithData(token, c)
		return
	}
}

func ChangePwd(c *gin.Context) {
	var changePwdReq dto.ChangePwd
	if err := c.ShouldBindJSON(&changePwdReq); err != nil {
		ReturnFailed(err.Error(), c)
		return
	}
	if err := service.ChangeUserPwd(changePwdReq); err == nil {
		ReturnSuccess(c)
	} else {
		ReturnFailed(err.Error(), c)
	}
}

/**
发送重置密码的链接
*/
func SendResetPwdLink(c *gin.Context) {
	var req dto.GetResetPwdLinkReq
	if err := c.ShouldBindJSON(&req); err != nil {
		ReturnFailed(err.Error(), c)
		return
	}
	if err := service.MakeUserForgetVerifyKey(req.Email); err != nil {
		ReturnFailed(err.Error(), c)
		return
	}
	ReturnSuccess(c)
}

func ResetPwd(c *gin.Context) {
	var req dto.ResetPwdReq
	if err := c.ShouldBindJSON(&req); err != nil {
		ReturnFailed(err.Error(), c)
		return
	}
	if err := service.ResetUserPwd(req); err != nil {
		ReturnFailed(err.Error(), c)
		return
	}
	ReturnSuccess(c)
}

// 更新用户信息
func UpdateUserinfo(c *gin.Context) {
	if err := CheckSession(c); err != nil {
		ReturnFailed(err.Error(), c)
		return
	}
	var updateUserinfo dto.UpdateUserinfo
	if err := c.ShouldBindJSON(&updateUserinfo); err != nil {
		ReturnFailed(err.Error(), c)
		return
	}

	//TODO 更新用户信息

}
